//
//  MineViewController.m
//  TimeBoat
//
//  Created by yang on 2022/7/5.
//  Copyright © 2022 yang. All rights reserved.
//

#import "MineViewController.h"
#import "AccountManager.h"

@interface MineViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *userImg;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLab;


@end

@implementation MineViewController


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hideNaviBar = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userInformationHasUpdate) name:kUserDidLoginNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userInformationHasUpdate) name:kUserDidUpdateNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userInformationHasUpdate) name:kUserDidLogoutNotification object:nil];
    
    [self setUserInfo];
}

- (void)userInformationHasUpdate{
    [self setUserInfo];
}

- (void)setUserInfo{
    NSString *nickname = [AccountManager shareAccount].loginModel.nickname;
    NSString *avatarUrl = [AccountManager shareAccount].loginModel.avatarUrl;
    NSString *gender = [AccountManager shareAccount].loginModel.gender;
    
    if (nil == nickname) {
        nickname = @"";
    }
    
    self.nickNameLab.text = nickname;
    
    if([gender isEqualToString:@"1"]) {
        [self.userImg sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:[UIImage imageNamed:@"information_icon_boy"]];
    } else {
        [self.userImg sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:[UIImage imageNamed:@"information_icon_girl"]];
    }
}


- (IBAction)clickUserAvatar:(UITapGestureRecognizer *)sender {
    if (![self judgeLogin]) {
        [self presentViewController:[self getNavigationController] animated:YES completion:nil];
        return;
    }
    
    PUSH_TO_VC(@"UserInfoViewController");
}


#pragma mark - 设置
- (IBAction)setupClick:(UIButton *)sender {
    if (![self judgeLogin]) {
        [self presentViewController:[self getNavigationController] animated:YES completion:nil];
        return;
    }
    
    UIViewController *vc = [SetupViewController viewController];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    //PUSH_TO_VC(@"SetupController");
}


#pragma mark - 点击账号与资料
- (IBAction)userInformationClick:(UIButton *)sender {
    if (![self judgeLogin]) {
        [self presentViewController:[self getNavigationController] animated:YES completion:nil];
        return;
    }
    PUSH_TO_VC(@"UserInfoViewController");
}



- (IBAction)aboutClick:(UIButton *)sender {
    if (![self judgeLogin]) {
        [self presentViewController:[self getNavigationController] animated:YES completion:nil];
        return;
    }
    
    UIViewController *vc = [AboutViewController viewController];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


- (UINavigationController *)getNavigationController{
    LoginViewController *loginCtr = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtr];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    return nav;
}

- (BOOL)judgeLogin{

    if ([NSString hhe_isEmpty:[AccountManager shareAccount].loginModel.token]) {
        return NO;
    }
    else{
        return YES;
    }
}
@end
